package com.mall.sun.controller;


import com.mall.sun.model.vo.ResultVO;
import com.mall.sun.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * description:  <br>
 * date: 2021/8/28 18:26 <br>
 * author: ztz <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @PostMapping("/upload")
    public ResultVO upload(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            return new ResultVO("上传失败",400,"上传的为空");
        }
        String fileName = file.getOriginalFilename();
        //String  filePath = "F:\\素材箱\\图片\\upload\\";
        String  filePath = "/var/upload/";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            return CommonUtil.ajaxReturn("http://localhost:8081/images/"+fileName);
        } catch (IOException e) {
           e.printStackTrace();
        }
        return new ResultVO("上传失败",500,"服务器内部异常");
    }
}
